package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.constant.RoleType;
import com.cloudfun.campusshare.common.model.dto.BatchOperateDTO;
import com.cloudfun.campusshare.common.model.dto.CouponCreateDTO;
import com.cloudfun.campusshare.common.model.dto.CouponSearchDTO;
import com.cloudfun.campusshare.common.model.vo.CouponListVO;
import com.cloudfun.campusshare.common.model.vo.CouponSimpleVO;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by CodeGenerator on 2020/06/05
 */
@Slf4j
@RestController
@AuthRequired
@RequestMapping("/v1/Coupon")
@Api("优惠券模块")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @PostMapping
    @ApiOperation("创建")
    @AuthRequired(role = RoleType.OPERATIONAL_MERCHANT)
    public BaseResult<?> create(@RequestBody @Valid CouponCreateDTO createDTO) {
        couponService.create(createDTO);
        return BaseResultFactory.produceSuccess();
    }

    @GetMapping("/list")
    @ApiOperation("分页列表")
    BaseResult<PageResult<CouponListVO>> search(Pageable pageable, CouponSearchDTO searchDTO) {
        return BaseResultFactory.produceResult(Code.SUCCESS, couponService.search(searchDTO, pageable));
    }

    @GetMapping("/simpleList")
    @ApiOperation("简单列表")
    BaseResult<List<CouponSimpleVO>> simpleList(CouponSearchDTO searchDTO) {
        return BaseResultFactory.produceResult(Code.SUCCESS, couponService.simpleList(searchDTO));
    }

    @DeleteMapping
    @ApiOperation("删除")
    @AuthRequired(role = RoleType.OPERATIONAL_MERCHANT)
    BaseResult<?> delete(@ApiParam(value = "id", required = true) @RequestParam String id) {
        couponService.delete(id);
        return BaseResultFactory.produceEmptyResult(Code.SUCCESS);
    }

    @DeleteMapping("/batch")
    @ApiOperation("批量删除")
    @AuthRequired(role = RoleType.OPERATIONAL_MERCHANT)
    BaseResult<?> batchDelete(@ApiParam(value = "BatchOperateDTO", required = true) @RequestBody BatchOperateDTO<CouponSearchDTO> batchOperateDTO) {
        if (CollectionUtils.isEmpty(batchOperateDTO.getIds()) && null == batchOperateDTO.getSearchData() && !BooleanUtils.isTrue(batchOperateDTO.getAll())) {
            return BaseResultFactory.produceEmptyResult(Code.PARAMETER_ERROR, "参数错误");
        }
        Map<Code, Integer> res;
        if (!CollectionUtils.isEmpty(batchOperateDTO.getIds())) {
            res = couponService.delete(batchOperateDTO.getIds());
        } else if (null != batchOperateDTO.getSearchData()) {
            res = couponService.delete(batchOperateDTO.getSearchData());
        } else {
            res = couponService.deleteAll();
        }
        return BaseResultFactory.produceResult(Code.SUCCESS, res);
    }

}